<script>
    layui.use(['layer', 'table', 'form','laydate'], function () {
        var table = layui.table;
        var form = layui.form;
        var layer = layui.layer;
        var laydate = layui.laydate;

        var dataTable = table.render({
            elem: '#dataTable'
            , url: "{{route('admin.websitesTopic.listData')}}?id={{$id}}"
            , defaultToolbar: []
            ,toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
            , page: true
            , cols: [[
                {field: 'id', title: 'ID', width: 60, fixed: 'left'}
                , {field: 'titles', title: '栏目名称'}
                , {field: 'article_count',width: 200, title: '文章数量'}
                , {field: 'sort',width: 200, title: '显示顺序'}
                , {field: 'statusName',width: 100, title: '状态', templet: '#switchTpl'}
                ,{field: 'created_at', width: 210, title: '添加时间', sort: false}
                , {title: '操作', width: 350, align: 'center', toolbar: '#options'}
            ]]
        });
        //日期时间范围
        laydate.render({
            elem: '#test10'
            ,type: 'datetime'
            ,range: true,
            isclear: false, //是否显示清空
        });

        $('#searchForm').click(function () {
            var titles = $('#titles').val();
            dataTable.reload({
                where: {
                    titles: titles
                },
                page: {curr: 1}
            });
        });

        //头工具栏事件
        table.on('toolbar(dataTable)', function(obj){
            if(obj.event === "refresh"){
                layui.table.reload('dataTable');
            }else if(obj.event === "add"){
                var index = layer.open({
                    title: "添加",
                    skin: 'layui-layer-molv',
                    icon: '<i class="fa fa-user-circle-o"></i>',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['35%', '45%'],
                    content: "{{route('admin.websitesTopic.createForm')}}?id={{$id}}",
                    end: function () {
                        window.location.reload();
                    }
                });
            }
        });

        //修改
        table.on('tool(dataTable)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                var index = layer.open({
                    title: "编辑",
                    icon: '<i class="fa fa-user-circle-o"></i>',
                    skin: 'layui-layer-molv',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['35%', '45%'],
                    content: "{{route('admin.websitesTopic.editForm')}}?id="+data.id,
                    end: function () {
                        $(".layui-laypage-btn").click();
                    }
                });
                return false;
            }
        });

    });

</script>
